<--- %%NOBANNER%% --> mergecells.sas
 BackForward

/*-------------------<---Start of Description-->---------------------\
| In word document, to merge cells within a table horizontally or    |
| vertically;                                                        |
|---------------------<---End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<---Start of Files or Arguments Needed-->---------------|
|  position=rN1cN2:rN3cN4;                                           |
|       the merge start position is the N1th row and N2th column;    |
|       the merge end position is the N3th row and N4th column;      |
|  nrows: number of rows to be merged;                               |
|  ncols: number of columns to be merged;                            |
|  wordref: word reference; not necessary default is "wordsys";      |
|  Note: if row number and column number after ":" (rN3cN4) is not   |
|        provided, user can alternatively specify number of rows to  |
|        be merged ("nrows=") and number of columns to be merged     |
|        ("ncols=") to accomplish similar result;                    |
|        So it is not necessary to specify in both ways;             |
|  Note: the merge end row and end columns must be larger then start |
|        row and start column (the end position must be on the right |
|        down direction of the start position), which means the nrows|
|        and ncols must be greater than or equal to 0;               |
|-------------<---End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<---Start of Files Created-->---------------------|
|  Example: %mergecells(position=r2c3:r3c43 r5c6:r6:c8); /           |
|           %mergecells(position=r2c3, nrows=3, ncols=2);            |
|  Usage:   %mergecells(position=,nrows=1,ncols=1,wordref=wordsys);  |
\-------------------<---End of Files Created-->---------------------*/
%macro mergecells(position=,nrows=,ncols=,wordref=wordsys);
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  2-27-2001 7:33pm;                 |
| Modified: 7-15-2001 10:10pm;                |
| Purpose:  Merge cells of the table in the   |
|           word document;                    |
\--------------------------------------------*/
%local nrows ncols numrows numcols _ir_ _ic_ _i_ _j_ _k_ npos _ij_;
%let npos=%words(&position);
%do _ij_=1 %to &npos;
%let position&_ij_=%qscan(&position,&_ij_,%str( ));

%let startpos=%qscan(&&position&_ij_,1,%str(():, ));
%let startrowpos=%qscan(&startpos,1,%str(r c R C));
%let startcolpos=%qscan(&startpos,2,%str(r c R C));
%let endpos=%qscan(&&position&_ij_,2,%str(():, ));
%let endrowpos=%qscan(&endpos,1,%str(r c R C));
%let endcolpos=%qscan(&endpos,2,%str(r c R C));
%if (&endrowpos ne) and (&endcolpos ne) %then %do;
   %let nrows=%eval(&endrowpos-&startrowpos);
   %let ncols=%eval(&endcolpos-&startcolpos);
%end;
%else %do;
   %if (%quote(&nrows) ne) and (%quote(%chk_type(&nrows)) eq %quote(2)) %then %do;
      %if (%sysfunc(exist(&nrows))) %then %do;
         %let dsid=%sysfunc(open(&nrows));
         %let nrows=%sysfunc(attrn(&dsid,NOBS));
         %let rc=%sysfunc(close(&dsid));
      %end;
   %end;
   %if (%quote(&ncols) ne) and (%quote(%chk_type(&ncols)) eq %quote(2)) %then %do;
      %if (%sysfunc(exist(&ncols))) %then %do;
         %let dsid=%sysfunc(open(&ncols));
         %let ncols=%sysfunc(attrn(&dsid,NOBS));
         %let rc=%sysfunc(close(&dsid));
      %end;
   %end;
%end; 
%let numrows=%eval(&startrowpos-1);
%let numcols=%eval(&startcolpos-1);
%if (%quote(&nrows) ne) or (%quote(&ncols) ne) %then %do;
   %if (%quote(&nrows) ne) %then %do;
      %if &nrows <0 %then %do;
      %put ==> Alert! Either number of rows(=&nrows) is less than 0: ;
      %put ==>        row position(=&endrowpos) is less then start row;
      %put ==>        position(=&startrowpos).;
      %end;
   %end;
   %if (%quote(&ncols) ne) %then %do;
      %if &ncols <0 %then %do;
         %put ==> Alert! Either number of columns(=&ncols) is less than 0: ;
         %put ==>        column position(=&endcolpos) is less then start column;
         %put ==>        position(=&startcolpos).;
      %end;
   %end;
   /*%put ncols=&ncols;*/
   data _null_;
      file &wordref lrecl=2000;
      put '[TableSelectTable]';
      put '[CharLeft 1]';
      %do _ir_=1 %to &numrows;
         put '[TableSelectCell]'; 
         put '[EditGoTo .Destination = "l+1"]';
      %end;
      %do _ic_=1 %to &numcols;
         put '[TableSelectCell]'; 
         put '[NextCell]';
      %end;
   %if (%quote(&ncols) ne) %then %do;
      %if &ncols >0 %then %do;
            %do _i_=1 %to &ncols;
               put '[TableSelectCell]'; 
               put '[CharRight 1, 1]';
            %end;
         %end;
      %end;
      %if (%quote(&nrows) ne) %then %do;
         %if &nrows >0 %then %do;
            %do _j_=1 %to &nrows;
               put '[TableSelectCell]'; 
               put '[LineDown 1, 1]';
            %end;
         %end;
      %end;
      put '[TableMergeCells]';
      put '[TableRowHeight .RulerStyle = "0", .LineSpacingRule = 0,
                  .LineSpacing = "", .LeftIndent = "0" + Chr$(34),
                  .Alignment = 0, .AllowRowSplit = 0]';
   run;
%end;
%else %do;
   %put ==> Alert! Check your input: nrows or ncols has no values. ;
%end;
%end;
%mend mergecells;